﻿// JScript 文件

//function init()
//{
//    var tc = new CtnTabCtrl();
//    tc.create("Tab_Bar");
//    tc.onTab = function(data)
//    {
//        alert(data);
//    }
//    for (var i = 1; i < 10; i++)
//    {
//        tc.append("标题" + i,  1);
//    }
//}
function CtnTabCtrl()
{
    this.arrTab = new Array();
    this.elemTab = null;
    this.elemBar = null;
    this.bRemove = false;
    this.tabcss = "tabctrl";
    this.barcss = "tabctrlbar";
    this.curcss = "current";
    this.overcss = "hover";
    this.curElem = null;
    
    this.onTab = null;
    
    this.create = create_Tab;
    this.append = append_Tab;
    this.update = update_Tab;
    this.remove = remove_Tab;
    this.setActive = setActive_Tab;
    this.getCurData = getCurData_Tab;
}

function create_Tab(divId)
{
    var elemDiv = document.getElementById(divId);
    if (!elemDiv)
    {
        alert("不存在" + divId);
        return false;
    }
    elemDiv.innerHTML="";
    var elemTab = document.createElement("ul");
    elemTab.className = this.tabcss;
    elemDiv.appendChild(elemTab);
    this.elemTab = elemTab;
//    var elemBar = document.createElement("ul");
//    elemBar.className = this.barcss;
//    document.body.appendChild(elemBar);
//    this.elemBar = elemBar;
}

function append_Tab(name, data, wndId)
{
    var elemLi = document.createElement("li");
    this.elemTab.appendChild(elemLi);
    elemLi.data = data;
    if (wndId)
    {
        elemLi.elemWnd = document.getElementById(wndId);
        elemLi.elemWnd.style.display = "none";
    }
    elemLi.innerHTML = name;
    elemLi.object = this;
    elemLi.onclick = function()
    {
        elemLi.object.setActive(this);
    }
    elemLi.onmouseover = function()
    {
        this.className = this.className + " " + this.object.overcss;
    }
    elemLi.onmouseout = function()
    {
        this.className = this.className.replace(this.object.overcss, "");
    }
    if (this.bRemove)
    {
        elemLi.ondblclick = function()
        {
            elemLi.object.remove(this)
        }
    }
    
    this.arrTab.push(elemLi);
//    this.setActive(elemLi);
    return elemLi;
}

function update_Tab()
{
    
}

function remove_Tab(elem)
{
    
}

function setActive_Tab(elem)
{
    if (typeof(elem) == "number")
    {
        elem = this.arrTab[elem];
    }
    if (elem.className == this.curcss )
    {
        return;
    }
    if (this.onTab)
    {
        this.onTab(elem.data);
    }
    if (this.curElem)
    {
        this.curElem.className = this.curElem.className.replace(this.curcss, "");
        if (this.curElem.elemWnd)
        {
            this.curElem.elemWnd.style.display = "none";
        }
    }
    elem.className = elem.className + " " + this.curcss;
    if (elem.elemWnd)
    {
        elem.elemWnd.style.display = "block";
    }
    this.curElem = elem;
}

function getCurData_Tab()
{
    if (this.curElem)
    {
        return curElem.data;
    }
}